import {createStore} from "vuex";
import { getInc } from "@/api/vuexdemo";
import goods from './modules/goods'


export default createStore({
    modules:{
        goods1: goods,
    },
    state:{
        count:5,
        age:12,
        isLoading: false,
        
    },  
    getters:{
        isAllowMarry(state){
            return state.age>=22?"允许":"不允许";
        }
    },
    mutations:{
        
        showLoading(state){
            state.isLoading=true
        },
        hideLoading(state){
            state.isLoading=false
        },

        // state:就是仓库的state属性
        // payload:载荷。
        increment(state,payload){
            // console.log("payload",payload);
            state.count+=payload.inc
        },
        decrement(state,payload){
            state.count-=payload.dec
        }
        
    },
    actions:{
        // context：上下文对象，此处表示仓库对象。
        incrementAction(context,payload){
            getInc()
            .then((result) => {
                if (result.data) {
                    // 提交mutation
                    context.commit({
                        type: "increment",
                        inc: result.data.count
                    });

                }
            }).catch((err) => {
                console.log("服务器出错",err);
            });
        }
    }

})